var userid = getCookie("userid");
var token = getCookie("token");

//全局接口参数
var siteConfig = {
    //接口域名
    apiDomain: "http://192.168.1.162:8011/api/",
    imgDomain: "http://static.711688.net.cn/",
    imgPath: {
        widget: "http://static.711688.net.cn/upload/store/{storeid}/widget/",
        product: "http://static.711688.net.cn/upload/store/{storeid}/product/show/source/",
        news: "http://static.711688.net.cn/upload/news/",
        WeApp: "http://static.711688.net.cn/upload/store/{storeid}/weapp/",
        WeService: "http://static.711688.net.cn/upload/store/{storeid}/weservice/",
    }
}

//全局的接口地址
var siteApiList = {
    //员工信息
    GetInfo: siteConfig.apiDomain + "staff/getinfo",
    //员工标签
    GetStaffTagList: siteConfig.apiDomain + "staff/GetStaffTagList",
}
//全局的html跳转地址
var siteUrlList = {
    //员工标签页面
    StafTag: "stafftag.html"
}



//全局的json状态码
var jsonCode = { success: 0, error: 501, NoData: 17 };


function $_2(id) {
    return document.getElementById(id);
}

/*URL参数*/
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = decodeURI(window.location.search).substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return "";
}

//删除左右两端的空格
function trim(str) {
    if (str == null) return "";
    return str.replace(/(^\s*)|(\s*$)/g, "");
}

//跳转页面
function toUrl(url) {
    window.location.href = url;
}

//字符串是否空对象或者空值
function IsNullOrEmpty(str) {
    if (str == null || str == '')
        return true;
    return false;
}

//接口的数据是否正常
function isJsonSuccess(data) {
    if (data.Code != jsonCode.success) {
        Alert(data.Message);
        return false;
    }
    return true;
}

function IsLogin() {
    token = getCookie("token");
    userid = getCookie("userid");
    if (userid == undefined || userid == "" || userid == null) {
        return false;
    }
    return true;
}


//自动微信登录
function AuthLogin() {
    return login();
}

// 登录状态验证
function login() {
    if (!islogin()) {
        toUrl('/api/oauth/wxlogin.ashx?login');
        return false;
    }
    return true;
}


function GetDate(str) {
    if (str == null) return "";
    return str.replace("T", "");
}


//显示消息
function Alert(msg) {
    toast.show(msg);
}


/********** 异步请求 start ************/
function ajaxGet(url, callBack) {
    Ajax(url, "", "GET", callBack);
}

function ajaxPost(url, param, callBack) {
    Ajax(url, param, "POST", callBack);
}

function Ajax(url, paramet, method, callBack) {
    // wait.show("加载中");//显示等待框
    $.ajax({
        url: url,
        type: method,
        dataType: "json",
        data: encodeURI(paramet),
        success: function (json) {
            // wait.dismiss();
            callBack(json);     //json方式回调
        },
        error: function (rs) {
            toast.show("请求异常!");
        }
    });
}

/********** 异步请求 end ************/


/********** Android提示框 start ************/
var toast = new Toast();
function Toast() {

    //提示框的html代码
    var _toast_html = '<div id="floatTS"><div style="width: 100%; height: 100%; background-color: Black; position: fixed;z-index:1000; opacity: 0.3;display:none;"></div><div style="width: 100%; height: 45px; left: 0; top: 50%; position: fixed; z-index:99999999999;"align="center"><div id="TStxt" style = "padding:10px; border-radius:5px; line-height:30px; background-color:Black; color:White; width:200px; display:block; opacity:0.8;"></div></div></div>';
    var maxOpacity = 100; //透明度的最大值
    var opacity = 0; //当前透明度值
    var tWaite = 1000; //弹出提示停留时间(毫秒)
    var tSpeed = 10; //提示框小时速度
    var disOpacity = 0.8; //显示时透明度值

    this.show = function (msg) {

        //先停止等待
        wait.dismiss();

        //方式三 
        if (self != top) {
            window.parent.window.toast.show(msg);//调用父窗口的toast对象
            return;
        }
        if ($("#TStxt").length < 1) {
            $("body").prepend(_toast_html);
        }
        else {
            $("#floatTS").css("display", "");
        }
        $("#TStxt").text(msg);
        if (opacity < 1) {
            opacity = maxOpacity;
            this.dismiss();
        } else {
            opacity = maxOpacity;
        }
    }

    //隐藏
    this.dismiss = function () {

        if (opacity < 0) {
            $("#floatTS").css("display", "none");
            return false;
        }
        $("#TStxt").css("opacity", opacity / maxOpacity * disOpacity);
        if (opacity == maxOpacity) {
            opacity--;
            setTimeout(function () { toast.dismiss(); }, tWaite);
        }
        else {
            opacity--;
            setTimeout(function () { toast.dismiss(); }, tSpeed);
        }
    }
}

/********** Android提示框 end ************/

/********** 等待对话框 start ************/

var wait = new WaitDialog();
function WaitDialog() {
    //等待框html代码
    var _wait_html = '<div id="floatWait"><div style="width: 100%; height: 100%; background-color: Black; position: fixed;z-index: 9999; opacity: 0.3;display:none;"></div><div style="width: 100%; height: 45px; left: 0; top: 50%; position: fixed; z-index: 1000;"align="center"><div style = "padding:10px; border-radius:5px; line-height:30px; background-color:Black; color:White; width:200px; display:block; opacity:0.8;"><span id = "msg"></span><span id="dot"></span></div></div></div>';
    this.isShow = false;
    this.show = function (msg) {
        if ($("#dot").length < 1) {
            $("body").prepend(_wait_html);
        }
        else {
            $("#floatWait").css("display", "");
        }
        $("#msg").text(msg);
        if (!this.isShow) {
            this.isShow = true;
            this.action();
        }
    }

    //动画
    this.action = function () {
        if ($("#dot").text().length > 5)
            $("#dot").text("·");
        else
            $("#dot").text($("#dot").text() + "·");

        if (this.isShow) {
            setTimeout(function () { wait.action(); }, 500);
        }
    }

    this.dismiss = function () {
        this.isShow = false;
        $("#floatWait").css("display", "none");
    }
}

/********** 等待对话框 start ************/

/********** 缓存 start ************/
//设置cookies
function SetCookie(name, value, Days) {
    if (Days == null) Days = 30; //设置Cookie有效时间
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/";
}

//获取cookies
function getCookie(name) {
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]); return null;
}

//删除cookies
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null) {
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/";
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    }
}

//缓存到磁盘
function setStorage(key, value) {
    localStorage.setItem(key, value);
}

//读取缓存到磁盘的数据
function getStorage(key) {
    return localStorage.getItem(key);
}

//删除缓存
function delStorage(key) {
    if (key != null) {
        localStorage.removeItem(key);
    } else {
        localStorage.clear();
    }
}

//缓存到磁盘
function setSession(key, value) {
    sessionStorage.setItem(key, value);
}

//读取缓存到磁盘的数据
function getSession(key) {
    return sessionStorage.getItem(key);
}

//删除缓存
function delSession(key) {
    if (key != null) 
        sessionStorage.removeItem(key);
    else 
        sessionStorage.clear();
}
/********** 缓存 end ************/


//返回
function goBack() {
    var refurl = document.referrer.toString().toLocaleLowerCase();
    var backUrl = getQueryString("backurl");

    if (backUrl != null) {
        window.location.href = backUrl; //退回到上一页
        return;
    }

    if (refurl != null && refurl != '') {
        window.location.href = refurl;
        return;
    }

    window.location.href = 'index.html';
}

function isEmpty(data, msg) {
    //只输出错误消息
    if (data.code != jsonCode.success)
        msg = IsNullOrEmpty(msg) ? data.msg : msg;

    if (data.code != JsonCode.success) {
        Alert(msg);
        return true;
    }

    //列表没数据,显示接口msg
    if (data.code == jsonCode.NoData) {
        Alert(msg);
        return true;
    }

    //列表没数据,显示错误消息
    if (data.code == jsonCode.success && (data.item != null && data.item.length == 0) && msg != '') {
        Alert(msg);
        return true;
    }
    return false;
}

//加载底部导航
function loadMenuBtn(index) {
    $(".menu .tab_1").bind("click", function () { toUrl('index.html') });
    $(".menu .tab_2").bind("click", function () { toUrl('channel.html') });
    $(".menu .tab_3").bind("click", function () {
        if (!IsVIP()) { return; }
        toUrl('qy_msg.html')
    });
    $(".menu .tab_4").bind("click", function () { toUrl('cp_index.html') });
    $(".menu .tab_5").bind("click", function () {
        if (!islogin()) { authLogin(); return; }
        toUrl('my.html')
    });
}